package com.li.controller;

import com.github.pagehelper.PageInfo;
import com.li.DTO.CommonResult;
import com.li.entity.Book;
import com.li.mapper.BookMapper;
import com.li.service.BookService;
import com.li.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Log4j2
@RequestMapping(path = "/book")
@RestController
@Api(tags = "书籍菜单")
public class BookController {
    @Resource
    BookMapper bookMapper;
    @Resource
    RedisUtil redisUtil;
    @Resource
    BookService bookService;

    @ApiOperation(value = "获取图书列表")
    @PostMapping("/bookList")
    public Object bookList() {
        log.info("请求bookList数据");
        CommonResult commonResult = new CommonResult();
        PageInfo<Book> books = bookService.bookList();
        commonResult.setData(books.getList());
        return commonResult;
    }

    @ApiOperation(value = "从缓存中获取图书列表")
    @PostMapping("/getbookList")
    public Object getbookList() {
        CommonResult commonResult = new CommonResult();
        List<Book> bookList = (List<Book>) redisUtil.get("bookList");
        log.info("请求数据");
        commonResult.setData(bookList);
        return commonResult;
    }

    @ApiOperation(value = "更新图书数据")
    @PostMapping("/update")
    public Object update(@RequestBody Book book) {
        CommonResult commonResult = new CommonResult();
        System.out.println("更新数据");
        int i = bookMapper.updateByPrimaryKey(book);
        commonResult.setData(i);
        return commonResult;
    }

    @ApiOperation(value = "添加图书数据")
    @PostMapping("/insert")
    public Object insert(@RequestBody Book book) {
        CommonResult commonResult = new CommonResult();
        System.out.println("插入数据");
        int insert = bookMapper.insert(book);
        commonResult.setData(insert);
        return commonResult;
    }

    @ApiOperation(value = "删除图书数据")
    @PostMapping("/delete")
    public Object delete(Integer id) {
        CommonResult commonResult = new CommonResult();
        System.out.println("删除数据");
        int i = bookMapper.deleteByPrimaryKey(id);
        commonResult.setData(i);
        return commonResult;
    }

}
